package bai.publicwelfare_backend.config;

import bai.publicwelfare_backend.interceptors.AuthInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    private final AuthInterceptor authInterceptor;

    public WebConfig(AuthInterceptor authInterceptor) {
        this.authInterceptor = authInterceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 拦截所有/product/**路径的请求
        registry.addInterceptor(authInterceptor)
                .addPathPatterns("/activity/**")
                // 可以排除一些不需要拦截的路径
                .excludePathPatterns("/auth/**","/token/**", "/activity/obtain");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 匹配所有后端 API 路径
                .allowedOrigins("*")  // 允许所有来源访问
                .allowedMethods("*"); // 限制可用的 HTTP 方法（如 GET/POST）
//                .allowedHeaders("Authorization", "Content-Type") // 是否允许携带 Cookie（需配合具体 Origin 使用）
//                .allowCredentials(true) //
//                .maxAge(3600); //
    }

}
